package com.kayak.easysp.util;

import io.netty.util.internal.StringUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.kayak.dubbo.bean.ConsumerBean;
import com.kayak.dubbo.global.RpcGlobal;
import com.kayak.easysp.transbase.TransBean;
import com.kayak.kkutils.CalendarUtil;
import com.kayak.kkutils.idGenerator.IdGenerator;
import com.kayak.kkweb.sys.Global;

public class ConsumerBeanUtil {
	private static IdGenerator idg=new IdGenerator();
	/**
	 * 传给服务层时  需要传递一个RpcBean 的继承类 ConsumerBean
	 * @param transBean
	 */
	public static ConsumerBean getConsumerBean(TransBean transBean){
		
		//交易流水
		String trans_id =StringUtil.isNullOrEmpty(transBean.getLocal(RpcGlobal.TRANS_ID)) ? idg.nextId() : transBean.getLocal(RpcGlobal.TRANS_ID);
		//用户id
		String cust_id = transBean.getLocalParam("user_id")==null?"":(String)transBean.getLocalParam("user_id");
		//交易码
		String transCode = (String)transBean.getLocalParam(TransConstEasysp.TRANS_CODE);
		//时间
		String timeStamp =CalendarUtil.getDateTime() ;
		//系统编号
		String system_id =Global.getGlobalConf("Global.G_SYSTEM_ID") ;
		//渠道类型
		//String channel_id =(String)transBean.getLocalParam(TransConstEasysp.CHANNEL_ID) ;
		String channel_id =Global.getGlobalConf("Global.G_CHANNEL_ID") ;
		
		ConsumerBean consumerBean=new ConsumerBean(trans_id,cust_id,transCode,timeStamp,system_id,channel_id,TransConstEasysp.NODE_ID);
		
		//设备信息号
		
		consumerBean.addRequest("head_osnumber", transBean.getLocalParam("head_osnumber"));
		consumerBean.addRequest("head_channel_type", transBean.getLocalParam("head_channel_type"));
	
		return consumerBean;
	}
	
	/**
	 * @param rpcBean
	 * @param objs 从rpcBean的localmap中获取同名参数，放到newConsumerBean的request中
	 * @return
	 */
	public static ConsumerBean getConsumerBean(TransBean transBean, String[] objs) {
		
		ConsumerBean consumerBean=getConsumerBean(transBean);
		//设备信息号
		consumerBean.addRequest("head_osnumber", transBean.getLocalParam("head_osnumber"));
		consumerBean.addRequest("head_channel_type", transBean.getLocalParam("head_channel_type"));
		
		for(String key : objs){
			consumerBean.getRequest().put(key, transBean.getLocalParam(key));
		}
		
		return consumerBean; 
	}
	
	/**
	 * 构建一个空的ConsumerBean
	 * @param transBean
	 */
	public static ConsumerBean getConsumerBean( Map<String, Object> localMap ){
		ConsumerBean  consumerBean= new ConsumerBean();
		consumerBean.setRequest(localMap);
		return consumerBean;
	}
	
	/**
	 * 还原 ConsumerBean原有值
	 * @param transBean
	 */
	public static ConsumerBean getConsumerBean( ConsumerBean consumerBean,TransBean transBean){
		ConsumerBean  newBean = ConsumerBeanUtil.getConsumerBean(transBean.getLocalMap());
		if(consumerBean !=null){
			newBean.getRequest().putAll(consumerBean.getRequest());
		}else{
			newBean.getRequest().putAll(ConsumerBeanUtil.getConsumerBean(transBean).getRequest());
		}
		return newBean;
	}

	public static void main(String[] args) {  
		Map  temp1= new HashMap<String, Object> ();
		temp1.put("1", "1");
		temp1.put("2", "2");
		temp1.put("CHANNEL_ID", "CHANNEL_ID");
		TransBean transBean = new TransBean();
		transBean.setRequest(temp1);
		transBean.getLocalMap().putAll(temp1);
		System.out.println("==transBean=="+transBean.getLocalParam("1"));
		
		ConsumerBean cb = null;
		cb=ConsumerBeanUtil.getConsumerBean(cb, transBean);
		System.out.println("==cb null=="+cb.getRequestParam("1"));
		cb.addRequest("CHANNEL_ID", "@@@");
		cb=ConsumerBeanUtil.getConsumerBean(cb, transBean);
		System.out.println("==cb =="+cb.getRequestParam("CHANNEL_ID"));

	}
	

}
